package com.groupbuying.store.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/05/28 19:00
 * @company 够样
 * @des
 **/
@Component
public class WeiXinUtils {


    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Value("${wx.appid}")
    private String appId;
    @Value("${wx.secret}")
    private String secret;
    @Value("${wx.accessTokenUrl}")
    private String accessTokenUrl;
    @Value("${wx.getUnlimitedUrl}")
    private String getUnlimitedUrl;
    @Value("${wx.accessTokenKey}")
    private String accessTokenKey;
    @Value("${wx.payPageStr}")
    private String payPageStr;
    @Value("${wx.paygyPageStr}")
    private String paygyPageStr;

    public String getAccessToken() {

        Object accessTokenObj = redisUtils.getValue(accessTokenKey);
        String accessToken = accessTokenObj!= null ? accessTokenObj.toString() : null;
        if (StringUtils.isBlank(accessToken)) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("grant_type", "client_credential");
            param.put("appid", appId);
            param.put("secret", secret);
            try {
                String result = httpClientUtils.doGet(accessTokenUrl, param);
                System.out.println("getAccessToken: " + result);
                Map<String, Object> resultMap = JSONUtils.jsonToMap(result);
                if (resultMap.containsKey("access_token") && resultMap.containsKey("expires_in")) {
                    accessToken = resultMap.get("access_token").toString();
                    long expireTime = Long.parseLong(resultMap.get("expires_in").toString()) - 100;
                    redisUtils.setKey(accessTokenKey, accessToken, expireTime);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return accessToken;

    }



    public  byte[] getWxcodeUnlimited(String accessToken, String counterUuid,String pageStr) {

        StringBuffer url = new StringBuffer();
        url.append(getUnlimitedUrl);
        url.append("?access_token=");
        url.append(accessToken);

        Map<String, Object> param = new HashMap<>();
        param.put("scene", "type=pay&counterUuid=" + counterUuid);
        param.put("page", pageStr);
        byte[] bytes = null;
        try {
           bytes = httpClientUtils.doPostSendJsonGetByte(url.toString(), param, null);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;

    }

    public byte[] getPayPageWxcode(String accessToken, String counterUuid){
        return this.getWxcodeUnlimited(accessToken,counterUuid,payPageStr);
    }

    public byte[] getPaygyPageWxcode(String accessToken, String counterUuid){
        return this.getWxcodeUnlimited(accessToken,counterUuid,paygyPageStr);
    }

}
